// pages/addeditAccount/addeditAccount.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // accountType: ['银行卡', '支付宝', '微信'],
    // accountType: ['银行卡'],
    // accountTypeI: 0,
    username: '',
    openingBank: [],
    bank_code: [],
    openingBankI: 0,
    bankcard: '',
    phonenum: '',
    idcard: '',
    openingaddress: '',
    agree: false,
    agreeShow: false,
    change: '',
    bank_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.getBanks();
    if (options.type == 1) {
      // 编辑
      wx.setNavigationBarTitle({
        title: '编辑银行卡'
      })
      that.setData({
        bank_id: options.bank_id,
        change: 1,
      });
      that.getBankinfo();
    } else if (options.type == 2) {
      // 添加
      wx.setNavigationBarTitle({
        title: '添加银行卡'
      })
      that.setData({
        change: 2,
      })
    }
  },





  // 选择账号类型
  accountTypeChange: function(e) {
    this.setData({
      accountTypeI: e.detail.value
    })
  },

  // 选择开户行
  openingBankChange: function(e) {
    this.setData({
      openingBankI: e.detail.value
    })
  },

  // 服务协议
  agreement: function() {
    this.setData({
      agreeShow: true
    })
  },

  //同意协议
  selectagree: function() {
    this.setData({
      agree: !this.data.agree
    })
  },

  // 同意服务条款
  agreeclause: function() {
    this.setData({
      agreeShow: false,
      agree: true
    })
  },

  // 确认
  formSubmit: function(e) {
    var that = this;
    if (that.data.agree == false) {
      wx.showToast({
        title: '请同意服务协议',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (e.detail.value.change == 1) {
      if (e.detail.value.username.length == 0) {
        app.showToast('请输入姓名');
        return false;
      }
      if (e.detail.value.bankcard.length == 0) {
        app.showToast('请输入银行卡号');
        return false;
      }
      if (e.detail.value.phonenum.length == 0) {
        app.showToast('请输入手机');
        return false;
      }
      if (e.detail.value.idcard != undefined && e.detail.value.idcard.length == 0) {
        app.showToast('请输入身份证号码');
        return false;
      }
      if (e.detail.value.openingaddress != undefined && e.detail.value.openingaddress.length == 0) {
        app.showToast('请输入银行卡开户地址');
        return false;
      }
      // 编辑
      that.setData({
        username: e.detail.value.username == undefined ? '' : e.detail.value.username,
        bankcard: e.detail.value.bankcard == undefined ? '' : e.detail.value.bankcard,
        phonenum: e.detail.value.phonenum == undefined ? '' : e.detail.value.phonenum,
        idcard: e.detail.value.idcard == undefined ? '' : e.detail.value.idcard,
        openingaddress: e.detail.value.openingaddress == undefined ? '' : e.detail.value.openingaddress
      });
      that.editBank();
    } else if (e.detail.value.change == 2) {
      // 添加
      if (e.detail.value.username.length == 0) {
        app.showToast('请输入姓名');
        return false;
      }
      if (e.detail.value.bankcard.length == 0) {
        app.showToast('请输入银行卡号');
        return false;
      }
      if (e.detail.value.phonenum.length == 0) {
        app.showToast('请输入手机');
        return false;
      }
      if (e.detail.value.accountType == 0 && e.detail.value.idcard.length == 0) {
        app.showToast('请输入身份证号码');
        return false;
      }
      if (e.detail.value.accountType == 0 && e.detail.value.openingaddress.length == 0) {
        app.showToast('请输入银行卡开户地址');
        return false;
      }

      that.setData({
        username: e.detail.value.username,
        bankcard: e.detail.value.bankcard,
        phonenum: e.detail.value.phonenum,
        idcard: e.detail.value.idcard,
        openingaddress: e.detail.value.openingaddress
      });
      if (!that.data.agree) {
        app.showToast('请同意服务协议');
        return false;
      }
      that.addBank();
    }


  },
  /**
   * 获取银行列表
   */
  getBanks() {
    var that = this;
    app.ajax({
      url: 'Partner/Bank/getBanks',
      method: "POST",
      data: {},
      success: function(res) {
        var flag = res.data;
        // console.log(flag);
        if (flag.code < 0) {
          app.showToast(flag.message);
          return false;
        }
        var tmp_name = [];
        var tmp_code = [];
        for (var i = 0; i < flag.data.length; i++) {
          tmp_name.push(flag.data[i].bank_name);
          tmp_code.push(flag.data[i].bank_code);
        }
        that.setData({
          openingBank: tmp_name,
          bank_code: tmp_code
        });
      }
    })
  },

  // 获取银行卡信息
  getBankinfo() {
    var that = this;
    app.ajax({
      url: 'Salesman/Account/getBankInfo',
      method: "POST",
      data: {
        bank_id: that.data.bank_id
      },
      success: function(res) {
        if (res.data.code == 1000) {
          for (var k = 0; k < that.data.openingBank.length; k++) {
            if (that.data.openingBank[k] == res.data.data.bank_name) {
              that.data.openingBankI = k;
              that.setData({
                openingBankI: k,
              })
              break;
            }
          }
          that.setData({
            username: res.data.data.p_name,
            bankcard: res.data.data.p_sn,
            phonenum: res.data.data.phone,
            idcard: res.data.data.id_card,
            openingaddress: res.data.data.address,
            agree: true
          })
        }
      }
    })
  },

  // 编辑银行卡
  editBank: function() {
    var that = this;
    var bank_id = that.data.bank_id;
    var openingBankI = that.data.openingBankI;
    var bank_code = that.data.openingBank[openingBankI];
    var bank_name = that.data.openingBank[openingBankI];
    app.ajax({
      url: "Salesman/Account/editBank",
      data: {
        "bank_id": bank_id,
        // "account_type": type,
        "full_name": that.data.username,
        "bank_name": bank_name,
        "bank_code": bank_code,
        "card_number": that.data.bankcard,
        "tel": that.data.phonenum,
        "IDcard": that.data.idcard,
        "bank_address": that.data.openingaddress
      },
      success: function(res) {
        if (res.data.code == 1000) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  // 添加银行卡
  addBank: function() {
    var that = this;
    app.ajax({
      url: 'Salesman/Account/addBank',
      method: "POST",
      data: {
        // account_type: parseInt(that.data.accountTypeI) + 1,
        full_name: that.data.username,
        bank_name: that.data.openingBank[that.data.openingBankI],
        bank_code: that.data.bank_code[that.data.openingBankI],
        card_number: that.data.bankcard,
        tel: that.data.phonenum,
        IDcard: that.data.idcard,
        bank_address: that.data.openingaddress,
      },
      success: function(res) {
        var flag = res.data;
        app.showToast(flag.message);
        if (flag.code > 0) {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
          return false;
        }

      }
    })
  }

})